import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '../views/pages/Login.vue'
import Register from '../views/pages/Register.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login,
    },
    {
      path: '/Register',
      name: 'Register',
      component: Register,
    }
  ],
})

// 配置全局路由守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('user')
  
  if(!isLogin && (to.path !== '/Login' && to.path !== '/Register')) {
    next('/Login')
  } else if(isLogin) {
    next()
  } else {
    next()
  }
})
export default router
